/*
 * w5200.h
 *
 *  Created on: 2014-3-9
 *      Author: Rome
 */



#ifndef W5200_H_
#define W5200_H_
#include "alt_types.h"

#define W5200_PORT 1080
#define SOCKETS  8
#define W5200RT_INITED 1
#define W5200RT_SUCCESS 0
#define W5200RT_CLOSED -1
#define W5200RT_TIMEOUT -3
#define W5200RT_RBUF_NA -4
#define W5200RT_TO_BE_CLOSED -5
// global register address
#define	W5200_MR	0
#define W5200_GAR	1	// 4 bytes
#define	W5200_SUBR	5	// 4 bytes
#define	W5200_SHAR	9	// 6 bytes
#define	W5200_SIPR	0x0F	// 4 bytes
#define	W5200_IR	0x15	// 1 bytes
#define	W5200_IMR2	0x16	// 1 bytes
#define	W5200_VERISON	0x1F	// 1 bytes
#define	W5200_IR2	0x34	// 1 bytes
#define	W5200_PHY_STATUS	0x35	// 1 bytes
#define	W5200_IMR	0x36	// 1 bytes

// socket register address
#define	W5200_SN_MR		0x4000	// 1 bytes
#define	W5200_SN_CR		0x4001	// 1 bytes
#define	W5200_SN_IR		0x4002	// 1 bytes
#define	W5200_SN_SR		0x4003	// 1 bytes
#define	W5200_SN_PORT	0x4004	// 2 bytes
#define	W5200_SN_TX_FSR	0x4020	// 2 bytes
#define	W5200_SN_TX_RD	0x4022	// 2 bytes
#define	W5200_SN_TX_WR	0x4024	// 2 bytes
#define	W5200_SN_RX_RSR	0x4026	// 2 bytes
#define	W5200_SN_RX_RD	0x4028	// 2 bytes
#define	W5200_SN_RX_WR	0x402A	// 2 bytes
#define	W5200_SN_IMR	0x402C	// 2 bytes

#define	W5200_SN_RXMEM	0xC000	// 2 bytes
#define	W5200_SN_TXMEM	0x8000	// 2 bytes


#define W5200_SEND	0x20
#define W5200_RECV	0x40
#define W5200_RESET	0x80

#define TX_MEM_SIZE 2048
#define RX_MEM_SIZE 2048

///* TX memory Base Address */
#define TX_MEM		0x8000
///* RX Memory Base Address */
#define RX_MEM		0xc000

#define Sn_IR_SEND_OK			0x10		/**< complete sending */
#define Sn_IR_TIMEOUT			0x08		/**< assert timeout */
#define Sn_CR_CLOSE		0x10		//< close

void W5200GlobalInit(void);
alt_32 W5200Initial(alt_32 DealyTime, alt_u8 socket_n);
alt_32 W5200CheckEstablish(alt_32 DealyTime, alt_u8 socket_n);
void W5200CloseSocket(alt_u8 socket_n);
alt_32 W5200CheckLink(void);

alt_32 W5200CheckRecv(alt_u8 socket_n);
alt_32 W5200SendBuf(alt_u8* Pbuf, alt_u32 length, alt_u8 socket_n);
alt_32 W5200RecvBuf(alt_u8* Pbuf, alt_u32 length, alt_u8 socket_n);


void W5200SpiRead(alt_u32 Address, alt_u32 Length, alt_u8 * pbuf);
void W5200SpiWrite(alt_u32 Address, alt_u32 Length, alt_u8 * pbuf);

void ResetW5200();

#endif /* W5200_H_ */
